package cc.linker.steplink.crm.merchant.task;


import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchanth5.dao.SignDao;
import cc.linker.steplink.crm.merchanth5.pojo.SignPo;
import cc.linker.steplink.crm.merchanth5.service.redis.SignCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

@Component
public class SignTask {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SignDao signDao;

    @RedisLock("SignTask_setTime")
    @Scheduled(cron = "55 59 23 * * ?")
    public void runTask() {
        Set<String> keys = stringRedisTemplate.keys(SignCacheService.SIGN_INFO + "*");
        if(null!=keys && keys.size()>0){
            for(String key:keys){
                String[] s = key.split("_");
                if(s.length>1){
                    SignPo signpo = new SignPo();
                    signpo.setOrgId(s[1]);
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(new Date());
                    calendar.add(Calendar.DATE,1);
                    signpo.setUpdateTime(calendar.getTime());
                    signDao.setSignTime(signpo);
                }
            }
        }
    }
}
